<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>课程列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">任课教师</div>
                <div class="layui-inline">
                    <input type="text" name="adm_realname" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-inline">课程名称</div>
                <div class="layui-inline">
                    <input type="text" name="cou_name" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
            </div>

            <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>
            <script type="text/html" id="buttonTpl">
                {{#  if(d.adm_status == 1){ }}
                <button class="layui-btn layui-btn-xs">已启用</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">已禁用</button>
                {{#  } }}
            </script>
            <script type="text/html" id="table-useradmin-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>

                <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i-->
                <!--class="layui-icon layui-icon-delete"></i>删除</a>-->
                <!---->
            </script>
            <script type="text/html" id="courseStudent">
                <a lay-href="{:url ('admin/student/studentList')}?cou_id={{d.cou_id}}" class="layui-btn  layui-btn-xs">学生查看</a>
            </script>
            <script type="text/html" id="courseHomework">
                <a lay-href="{:url ('admin/homework/homeworkList')}?cou_id={{d.cou_id}}&XDEBUG_SESSION_START=10860" class="layui-btn  layui-btn-xs">作业查看</a>
            </script>

        </div>
    </div>
</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;

        //管理员管理
        table.render({
            elem: '#LAY-user-back-manage'
            , url: "{:url ('admin/course/courseList')}"
            , page: true
            , cols: [[
                {field: 'cou_id', width: 80, title: 'ID', sort: true, align: 'center'}
                , {field: 'adm_realname', title: '任课老师', align: 'center'}
                , {field: 'cou_name', title: '课程名称', align: 'center'}
                , {field: 'studentNum', title: '学生人数', sort:true,align: 'center'}
                , {field: 'cou_start_time', title: '开始时间',sort: true, align: 'center'}
                , {field: 'cou_end_time', title: '结束时间',sort: true, align: 'center'}
                , {title: '查看课程学生', width: 150, align: 'center',  toolbar: '#courseStudent', align: 'center'}
                , {title: '查看课程作业', width: 150, align: 'center',  toolbar: '#courseHomework', align: 'center'}
                , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin', align: 'center'}
            ]]
            , text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }

        });

        //监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-user-back-manage', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    'cou_name':field.cou_name,
                    "adm_realname": field.adm_realname
                }
            });
        });

        //监听工具条
        table.on('tool(LAY-user-back-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除此管理员？', function (index) {
                    //获取当前管理员的id
                    var adm_id = data.adm_id;
                    $.ajax({
                        url: "{:url ('admin/admin/adminDelete')}?adm_id=" + adm_id,
                        type: "post",
                        dataType: "json",
                        success: function (res) {
                            if (res.code == 1) {
                                layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                    obj.del();
                                });
                            } else {
                                layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                });
                            }
                        }
                    })
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                var cou_id=data.cou_id;
                layer.open({
                    type: 2
                    , title: '课程编辑'
                    , content: "{:url ('admin/course/courseEdit')}?cou_id="+cou_id
                    , area: ['900px', '600px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-course-edit-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            $.ajax({
                                url: "{:url ('admin/course/courseEdit')}?XDEBUG_SESSION_START=18097&cou_id="+cou_id,
                                type: "post",
                                dataType: "json",
                                data:field,
                                success: function (res) {
                                    if (res.code == 1) {
                                        layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                            window.location.reload();
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                            // window.location.reload();
                                        });
                                    }
                                }
                            })
                            // table.reload('LAY-user-back-manage'); //数据刷新
                            layer.close(index); //关闭弹层
                        });
                        submit.trigger('click');
                    }
                    , success: function (layero, index) {

                    }
                })
            }
        });

        //事件(table上面的添加功能)
        var active = {
            add: function () {
                layer.open({
                    type: 2
                    , title: '添加课程'
                    , content: "{:url ('admin/course/courseAdd')}"
                    , area: ['900px', '600px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-course-add-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            console.log(field)
                            // {cou_adm_id: "2",
                            // cou_name: "123",
                            // cou_start_time: "2019-07-10 08:40:07",
                            // cou_end_time: "2019-07-26 08:40:09",
                            // studentInCourse: "[{"value":1,"title":"江大哥"},{"value":2,"title":"江弟弟"},{"value":3,"title":"张头"}]"}
                            if(field.cou_adm_id==-1){
                                layer.msg("请选择任课教师", {icon: 5, time: 1500}, function () {
                                });
                                return false;
                            }else{
                                //拿到数据后进行ajax提交数据
                                //发起ajax请求
                                $.ajax({
                                    url: "{:url ('admin/course/courseAdd')}?XDEBUG_SESSION_START=10860",
                                    type: "post",
                                    data: field,
                                    dataType: "json",
                                    success: function (res) {
                                        if (res.code == 1) {
                                            layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                                window.location.reload();
                                            });
                                        } else {
                                            layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                            });
                                        }
                                    }
                                })
                                layer.close(index); //关闭弹层
                            }

                        });

                        submit.trigger('click');
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>

